<template>
  <van-icon
    :color="value ? 'ffa500' : ''"
    :name="value ? 'star' : 'star-o'"
    @click="Collect"
  />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    acticleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {}
  },
  created () {},
  mounted () {},
  methods: {
    async Collect () {
      try {
        if (this.value) {
          //   已收藏，取消收藏
          await deleteCollect(this.acticleId)
        } else {
          // 没收藏，添加收藏
          await addCollect(this.acticleId)
        }
        // 更新视图
        this.$emit('input', !this.value)
        // 提示弹框
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
